<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>test</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #bg {
            width: 100%;
            height: 100vh;
            background: #F40;
            background-size: cover;
        }

        .btn {
            position: absolute;
            top: 80%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: #FFF;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
        }

        .tip {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            color: #FFF;
            font-size: 4rem;
            pointer-events: none;
        }
    </style>
</head>

<body>
    <div id="bg"></div>
    <span class="btn">再来一次</span>
    <p class="tip">等待颜色变化</p>
    <script>
        const bg = document.getElementById('bg');
        const btn = document.querySelector('.btn');
        const tip = document.querySelector('.tip');
        let changedTime = 0
        let timer = null;
        const test = () => {
            if(!bg.classList.contains('active')) return 
            if (tip.classList.contains('active')) return 
            if (changedTime) {
                const now = Date.now();
                console.log(now - changedTime)
                tip.innerHTML = now - changedTime + 'ms';
                tip.classList.add('active');
                // 解绑点击事件，防止重复触发
                bg.removeEventListener('click', test);
            }
        }
        const time = Math.random() * 5000 + 1000;
        bg.addEventListener('click', test)
        setTimeout(() => {
            bg.style.backgroundColor = '#00ff8f';
            bg.classList.add('active');
            changedTime = Date.now();
        }, time)

        btn.addEventListener('click', function () {
            location.reload();
        })
    </script>
</body>

</html>